#include <buola/scene.h>
#include <buola/scene/csceneview.h>
#include <buola/scene/ccamera.h>
#include <buola/scene/cperspectivecamera.h>
#include <buola/scene/cscene.h>
#include <buola/scene/geodes/ccube.h>
#include <buola/scene/geodes/ccylinder.h>
#include <buola/scene/geodes/ccapsule.h>
#include <buola/scene/transform/crttransform.h>
#include <buola/scene/cimagerenderer.h>
#include <buola/scene/cmesh.h>
#include <buola/scene/controllers/ccameracontroller.h>
#include <buola/image/io.h>
#include <buola/image/cgc.h>
#include <buola/gui/ctoolkit.h>
#include <buola/app/capp.h>
#include <buola/io/cresourcefinder.h>

using namespace buola;

int main(int pNArg,char **pArgs)
{
    buola_init(pNArg,pArgs);

    try
    {
        CApp lApp;

        gui::get_toolkit()->GetOpenGLGC()->MakeCurrent();
        
        //create the scene
        scene::PScene lScene=new scene::CScene;
        
        scene::PRTTransform lRoot=new scene::CRTTransform;
//    mGroup->SetRotation(mat::CQuat_d(M_PI_2,mat::CVec3d(0,0,1)));
        lRoot->SetTranslation(mat::CVec3d(0,0,0.185));
    
        scene::PGeode lGeode=scene::CGeode::Import(io::arg(0),0.001);
        lGeode->AttachTo(lRoot);

        lScene->World()->AddChild(lRoot);

        scene::PPerspectiveCamera lCamera=new scene::CPerspectiveCamera;
        lCamera->LookAt({0,0,0},{5,0,0},{0,0,1});
        lCamera->SetClipping(0.01,50.0);

        scene::CImageRenderer lRenderer;
        lRenderer.SetCamera(lCamera);
        lRenderer.AddScene(lScene);

        img::CImage_rgb8 lImage({640,640});
        
        lRenderer.GetImage(lImage);

        img::save(lImage,io::file("scene.ppm"));
        
        scene::PSceneView lView=new scene::CSceneView;
        lView->SetCamera(lCamera);
        lView->AddScene(lScene);
        lView->AddController(new scene::CCameraController(lCamera));
        lView->Map(CSize_d(800,600));
        
        lApp.Run();
    }
    catch(std::exception &pE)
    {
        msg_info() << pE.what() << "\n";
    }

    return buola_finish();
}
